package com.itheima.controller;

import com.aliyun.oss.AliOSSUtils;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class UploadController {
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传：{},{},{}", username, age, image);
//        String filename = image.getOriginalFilename();
//        int i = filename.lastIndexOf(".");
//        String suffix = filename.substring(i);
//        String uuid = UUID.randomUUID().toString();
//        String newFilename = uuid + suffix;
//        log.info("newFilename:{}",newFilename);
//        image.transferTo(new File("D:\\Study\\java_stu\\" + newFilename));
//        return Result.success();
//    }

    @Autowired
    private AliOSSUtils ossUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传，文件名：{}",image.getOriginalFilename());
        String re = ossUtils.upload(image);
        log.info("文件上传完成,url:{}",re);
        return Result.success(re);
    }
}
